package cm.unit4.theme3;

import cm.commom.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;

public class PrintFromTopToBottom {

    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        Queue<TreeNode> queue = new ArrayDeque<>(100);

        // queue的值不能为null
        if (root!=null){
            queue.add(root);
        }

        TreeNode node;
        while (!queue.isEmpty()){
            node = queue.remove();
            list.add(node.val);
            if (node.left!=null){
                queue.add(node.left);
            }
            if (node.right!=null){
                queue.add(node.right);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        PrintFromTopToBottom obj = new PrintFromTopToBottom();
        obj.PrintFromTopToBottom(null);
    }
}
